//
//  ClubTableViewController.swift
//  健身宝
//
//  Created by hzt on 2017/10/1.
//  Copyright © 2017年 hzt. All rights reserved.
//

//import UIKit
//
//class ClubTableViewController: UITableViewController {
//    
//    var clubList: NSArray = NSArray()
//    
//    override func viewDidLoad() {
//        super.viewDidLoad()
//        getClubList()
//        RONetworkMngTool.shareNetworkMngTool().RONetwork_GetClubListView(self.view) { (clubList: NSArray) -> Void in
//            self.clubList = clubList
//            self.tableView.reloadData()
//        }
//        
//        
//    }
//    
//    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return  200
//    }
//    
//    override func didReceiveMemoryWarning() {
//        super.didReceiveMemoryWarning()
//        // Dispose of any resources that can be recreated.
//    }
//    
//    // MARK: - Table view data source
//    
//    override func numberOfSections(in tableView: UITableView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 1
//    }
//    
//    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        // #warning Incomplete implementation, return the number of rows
//        if self.clubList.count == 0 {
//            return 0
//        }
//        return self.clubList.count
//        
//    }
//    
//        func getClubList(){
//            let urlStr = RO_GetClubList
//            let paraDict = ["sessionid":SessionID]
//            AFHTTPRequestOperationManager().post(urlStr, parameters: paraDict, success: { (response:AFHTTPRequestOperation?, data:Any?) in
//                let array = data as! NSArray
//                //print(array)
//                self.clubList = array.mutableCopy() as! NSMutableArray
//                //print(self.clubList)
//                self.tableView.reloadData()
//            }) { (response:AFHTTPRequestOperation?, error:Error?) in
//                print(response!.debugDescription)
//            }
//            
//        }
//    
//    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "clubListCell", for: indexPath)
//        
//        if self.clubList.count != 0 {
//            //let club: ROClubModel = self.clubList[(indexPath as NSIndexPath).row] as! ROClubModel
//            //cell.textLabel!.text = ROClubModel.Introduction
//            //使用afnetworking中的uiimageview加载网络图片
//            let dict = clubList[indexPath.row] as! NSDictionary
//            let pURL = "http://172.24.10.175/workout/Uploads/"
//            let Thumb = dict["thumb"] as? String
//            var imageUrl = URL(string: (pURL+Thumb!))
//            cell.imageView!.setImageWith(imageUrl, placeholderImage: UIImage(named: "placeHolder"))
//            cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
//        }
//        
//        return cell
//        
//    }

//    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        
//        let currentShop: ROShopModel = shopList[(indexPath as NSIndexPath).row] as! ROShopModel
//        self.performSegue(withIdentifier: "toFoodList", sender: currentShop)
//    }
    
//    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//        
//        let foodListController: ROFoodListTableViewController = segue.destination as! ROFoodListTableViewController
//        
//        foodListController.shop = sender as! ROShopModel
//    }
//}

import UIKit

class ClubTableViewController: UITableViewController,clubListDelegate {
    var clubList:NSMutableArray!
    var flag:Bool = true
    var ClubBtn:UIButton!
    var OtherBtn:UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        setView()
        getClubList()
    }
    
    func setView(){
        self.navigationController?.isNavigationBarHidden = false
        self.rdv_tabBarController?.isTabBarHidden = false
        tableView.backgroundColor = UIColor.lightGray
        tableView.refreshControl = UIRefreshControl()
        tableView.refreshControl?.addTarget(self, action: #selector(refreshData), for: .valueChanged)
        tableView.register(ClubListCell.self, forCellReuseIdentifier: "ClubListCell")
        tableView.register(ClubListCell.self, forCellReuseIdentifier: "ClubListCell")
        let headView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 40))
        ClubBtn = UIButton(frame: CGRect(x: 0, y: 0, width: view.bounds.width/2, height: 40))
        ClubBtn.backgroundColor = UIColor.gray
        ClubBtn.addTarget(self, action: #selector(showClubList), for: .touchUpInside)
        ClubBtn.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        ClubBtn.setTitle("最具人气", for: .normal)
        ClubBtn.titleLabel?.textAlignment = .center
        headView.addSubview(ClubBtn)
        
        OtherBtn = UIButton(frame: CGRect(x: view.bounds.width/2, y: 0, width: view.bounds.width/2, height: 40))
        OtherBtn.backgroundColor = UIColor.gray
        OtherBtn.addTarget(self, action: #selector(showClubList), for: .touchUpInside)
        OtherBtn.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        OtherBtn.setTitle("离我最近", for: .normal)
        OtherBtn.titleLabel?.textAlignment = .center
        headView.addSubview(OtherBtn)

        tableView.tableHeaderView = headView
    }
    
    func showClubList(){
        flag = true
        self.getClubList()
        tableView.reloadData()
        
    }
    
    func getClubList(){
        let urlStr = RO_GetClubList
        let paraDict = ["sessionid":SessionID]
        AFHTTPRequestOperationManager().post(urlStr, parameters: paraDict, success: { (response:AFHTTPRequestOperation?, data:Any?) in
            let array = data as! NSArray
            //print(array)
            self.clubList = array.mutableCopy() as! NSMutableArray
            //print(self.clubList)
            self.tableView.reloadData()
        }) { (response:AFHTTPRequestOperation?, error:Error?) in
            print(response!.debugDescription)
        }
        
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if flag && clubList != nil{
            return clubList.count
        }
        
        return 0
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let basicCell:UITableViewCell? = nil
        if flag && clubList != nil{
            let cell = tableView.dequeueReusableCell(withIdentifier: "ClubListCell", for: indexPath) as! ClubListCell
            cell.selectionStyle = .none
            let dict = clubList[indexPath.row] as! NSDictionary
            //print(dict)
            cell.indexPath = indexPath
            cell.clubDelegate = self
            let pURL = "http://172.24.10.175/workout/Uploads/"
            let Thumb = dict["thumb"] as? String
            var imageUrl = URL(string: (pURL+Thumb!))
            cell.ClubPhoto!.setImageWith(imageUrl, placeholderImage: UIImage(named: "placeHolder"))
            cell.Introduction.text =  dict["introduction"] as? String

        return cell
        }
        return basicCell!
    }
    
    func refreshData(){
        if flag{
            getClubList()
            tableView.reloadData()
            tableView.refreshControl?.endRefreshing()
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ->CGFloat {
        if flag{
            return 200
        }else{
            return 200
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
            let controller = ClubDetailViewController()
            controller.infoDict = clubList![indexPath.row] as! NSDictionary
            
            _ = navigationController?.pushViewController(controller, animated: true)
            
}

}
